package com.ytjj.qmyx.supplychain.common.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class EasyExcelUtil {

    public static <T> void createExcel(String filePath, List<T> dataList, String sheetName, Class<T> clazz) throws Exception {

        try {
            EasyExcel.write(filePath, clazz).sheet(sheetName).doWrite(dataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> void createExcelDynamicHead(String filePath, List<T> dataList, List<List<String>> head, String sheetName) throws Exception {
        try {
            EasyExcel.write(filePath).head(head).sheet(sheetName).doWrite(dataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> void createExcelManySheets(String filePath, Map<String, List<T>> dataMap, Class<T> clazz) throws Exception {
        ExcelWriter excelWriter = EasyExcel.write(filePath, clazz).build();
        try {
            int index = 0;
            for (Map.Entry<String, List<T>> entry : dataMap.entrySet()) {
                String sheetName = entry.getKey();
                List<T> data = entry.getValue();
                WriteSheet sheet = EasyExcel.writerSheet(index++, sheetName).build();
                excelWriter.write(data, sheet);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static <T> void createExcelWeb(OutputStream outputStream, List<T> dataList, String sheetName, Class<T> clazz) throws Exception{
        try {
            EasyExcel.write(outputStream, clazz).sheet(sheetName).doWrite(dataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> void createExcelDynamicHeadWeb(OutputStream outputStream, List<T> dataList, List<List<String>> head, String sheetName) throws Exception {
        try {
            EasyExcel.write(outputStream).head(head).sheet(sheetName).doWrite(dataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> void createExcelManySheetsWeb(OutputStream outputStream, Map<String, List<T>> dataMap, Class<T> clazz) throws Exception {
        ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz).build();
        try {
            int index = 0;
            for (Map.Entry<String, List<T>> entry : dataMap.entrySet()) {
                String sheetName = entry.getKey();
                List<T> data = entry.getValue();
                WriteSheet sheet = EasyExcel.writerSheet(index++, sheetName).build();
                excelWriter.write(data, sheet);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
}
